package csl.bluetooth.base;

import javax.bluetooth.RemoteDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Set;

/**
 * @author 陈少磊
 * @version 1.0.0
 * @ClassName BluetoothClient.java
 * @Description TODO 蓝牙客户端类
 * @createTime 2021年03月22日 19:30:00
 */
public class BluetoothClient {
    //流连接
    public StreamConnection streamConnection;
    //发现监听
    public OnDiscoverListener onDiscoverListener = null;
    //客户端监听
    public OnClientListener onClientListener = null;

    public RemoteDevice device;

    public DataInputStream dataInputStream;

    public OutputStream outputStream;

    /**
     * 是否连接了蓝牙设备
     */
    public boolean isConnect = false;

    /**
     * 客户端监听
     */
    public interface OnClientListener {
        void onConnected(DataInputStream inputStream, OutputStream outputStream) throws IOException;
        void onConnectionFailed();
        void onDisconnected();
        void onClose() throws IOException;
    }

    /**
     * 发现监听
     */
    public interface OnDiscoverListener {
        void onDiscover(RemoteDevice remoteDevice);
    }


    /**
     * 无参构造函数
     */
    public BluetoothClient() {
    }

    /**
     * 查找所有
     * @throws IOException
     * @throws InterruptedException
     */
    public void find() throws IOException, InterruptedException {
        //附近所有的蓝牙设备，必须先执行 runDiscovery
        Set<RemoteDevice> devicesDiscovered = RemoteDeviceDiscovery.getDevices();
        Iterator<RemoteDevice> itr = devicesDiscovered.iterator();
        //连接
        while (itr.hasNext()) {
            RemoteDevice remoteDevice = itr.next();
            onDiscoverListener.onDiscover(remoteDevice);
        }
    }

    /**
     * 启动连接
     * @param remoteDevice
     * @throws IOException
     * @throws InterruptedException
     */
    public String startClient(RemoteDevice remoteDevice) throws IOException, InterruptedException {
        System.out.println("开始连接蓝牙设备:"+remoteDevice.getFriendlyName(false));
        String url = "btspp://"+remoteDevice.getBluetoothAddress()+":1;authenticate=true;encrypt=true";
        System.out.println(url);
        this.device = remoteDevice;
        System.out.println(streamConnection);
        if (streamConnection==null){
                streamConnection = (StreamConnection) Connector.open(url);
                outputStream=streamConnection.openOutputStream();
                dataInputStream=streamConnection.openDataInputStream();
//                onClientListener.onConnected(dataInputStream,outputStream);
//                System.out.println("蓝牙连接成功！");
                isConnect = true;
                return "成功连接到蓝牙设备："+remoteDevice.getFriendlyName(false);
        }else{
            return "蓝牙已经连接!更换设备请重启工具选择对应的蓝牙设备！";
        }
    }

    public OnDiscoverListener getOnDiscoverListener() {
        return onDiscoverListener;
    }


    public void setOnDiscoverListener(OnDiscoverListener onDiscoverListener) {
        this.onDiscoverListener = onDiscoverListener;
    }


    public OnClientListener getClientListener() {
        return onClientListener;
    }


    public void setClientListener(OnClientListener onClientListener) {
        this.onClientListener = onClientListener;
    }

}
